package keqiang.com.kq_flutter_widgets.channel;

import android.content.Context;

import com.huawei.hms.ml.scan.HmsScan;
import com.keqiang.huaweiscan.CodeUtils;
import com.keqiang.huaweiscan.ScanConfig;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import androidx.annotation.NonNull;
import io.flutter.plugin.common.BinaryMessenger;
import io.flutter.plugin.common.MethodChannel;

/**
 * 原生扫码交互
 *
 * @author zhouzhuo810
 * @date 2023/3/3 15:10
 */
public class NativeScanChannel extends MethodChannel {
    
    // 通道id
    private static final String CHANNEL = "flutter.keqiang.com/scan";
    
    // 开始扫描
    private static final String METHOD_START_SCAN = "startScan";
    
    private Context mContext;
    
    public NativeScanChannel(@NonNull BinaryMessenger messenger) {
        super(messenger, CHANNEL);
        init();
    }
    
    public void setContext(Context context) {
        mContext = context;
    }
    
    private void init() {
        setMethodCallHandler((call, result) -> {
            switch (call.method) {
                case METHOD_START_SCAN:
                    //开始扫描
                    String title = call.argument("title");
                    String hint = call.argument("hint");
                    Boolean useAlbum = call.argument("useAlbum");
                    Boolean imgScanFailedBack = call.argument("imgScanFailedBack");
                    String imgScanFailedHint = call.argument("imgScanFailedHint");
                    List<Integer> scanTypes = call.argument("scanTypes");
                    Boolean turnOnLight = call.argument("turnOnLight");
                    Boolean showLightButton = call.argument("showLightButton");
                    startScan(title, hint, useAlbum != null && useAlbum,
                        imgScanFailedBack != null && imgScanFailedBack,
                        imgScanFailedHint, scanTypes, turnOnLight != null && turnOnLight,
                        showLightButton != null && showLightButton,
                        result);
                    break;
            }
        });
    }
    
    private void startScan(String title, String hint, boolean useAlbum, boolean imgScanFailedBack,
                           String imgScanFailedHint, List<Integer> scanTypes, boolean turnOnLight,
                           boolean showLightButton, MethodChannel.Result result) {
        ScanConfig config = ScanConfig.copy().title(title).hint(hint).couldScanImg(useAlbum)
            .imgScanFailedBack(imgScanFailedBack).imgScanFailedHint(imgScanFailedHint)
            .turnOnLight(turnOnLight)
            .showLightButton(showLightButton);
        if (scanTypes != null && !scanTypes.isEmpty()) {
            Integer format = null;
            List<Integer> formats = new ArrayList<>();
            for (Integer type : scanTypes) {
                if (type == 0) {
                    if (format == null) {
                        format = HmsScan.QRCODE_SCAN_TYPE;
                    } else {
                        formats.add(HmsScan.QRCODE_SCAN_TYPE);
                    }
                    
                    formats.add(HmsScan.DATAMATRIX_SCAN_TYPE);
                    formats.add(HmsScan.AZTEC_SCAN_TYPE);
                    formats.add(HmsScan.PDF417_SCAN_TYPE);
                } else if (type == 1) {
                    if (format == null) {
                        format = HmsScan.CODE39_SCAN_TYPE;
                    } else {
                        formats.add(HmsScan.CODE39_SCAN_TYPE);
                    }
                    
                    formats.add(HmsScan.CODE93_SCAN_TYPE);
                    formats.add(HmsScan.CODE128_SCAN_TYPE);
                    formats.add(HmsScan.EAN13_SCAN_TYPE);
                    formats.add(HmsScan.EAN8_SCAN_TYPE);
                    formats.add(HmsScan.ITF14_SCAN_TYPE);
                    formats.add(HmsScan.UPCCODE_A_SCAN_TYPE);
                    formats.add(HmsScan.UPCCODE_E_SCAN_TYPE);
                    formats.add(HmsScan.CODABAR_SCAN_TYPE);
                }
            }
            
            if (format != null) {
                int[] fors = new int[formats.size()];
                for (int i = 0; i < formats.size(); i++) {
                    fors[i] = formats.get(i);
                }
                config.formats(format, fors);
            }
        }
        
        CodeUtils.scan(mContext, config, scanResult -> {
            if (scanResult.isCancel()) {
                Map<String, Object> resultMap = new HashMap<>();
                resultMap.put("code", 2);
                resultMap.put("msg", "用户已取消");
                resultMap.put("data", null);
                result.success(resultMap);
                return;
            }
            HmsScan data = scanResult.getData();
            if (data == null || scanResult.getContents() == null) {
                Map<String, Object> resultMap = new HashMap<>();
                resultMap.put("code", 0);
                resultMap.put("msg", "未识别到内容");
                resultMap.put("data", null);
                result.success(resultMap);
                return;
            }
            String contents = scanResult.getContents();
            Map<String, Object> resultMap = new HashMap<>();
            resultMap.put("code", 1);
            resultMap.put("msg", "扫码成功");
            resultMap.put("data", contents);
            result.success(resultMap);
        });
    }
    
}
